package com.smbms.controller.cultvate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.smbms.pojo.cultvate.Cv_Feedback;
import com.smbms.pojo.cultvate.Cv_Plane;
import com.smbms.pojo.cultvate.Cv_Starts;
import com.smbms.service.cultivateservice.CvService;


/**
 * 培训管理模块
 * @author Administrator
 *
 */
@Controller
public class CultivateController {
	@Autowired
	private CvService cs;
	//跳转到培训计划页面
	@RequestMapping(value="/GetCvP.html")
	public String GetPlane(HttpServletRequest request) {
		List<Cv_Starts> cslist = cs.GetCs();//获取下拉框
		request.setAttribute("cvstatus", cslist);
		return "cultvate/cv_px";
	}
	
	//培训数据的展示兼模糊查询
	@RequestMapping(value="/GetAll.json",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String GetAll(Cv_Plane cp) {
		Map<String, Object> map = new HashMap<String ,Object>();
		List<Cv_Plane> cplist = cs.GetCv(cp);//获取培训计划所有数据
		map.put("data", cplist);
		return JSONArray.toJSONString(map);
	}
	//通过ID查询培训状态
	@RequestMapping(value="/SelectStartById.json",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String startByid(Cv_Plane csid) {
		List<Cv_Plane> lcp = cs.selectStartById(csid.getId());//通过ID获取培训状态
		JSONArray jsonArray = new JSONArray();// 鍒涘缓JSONArray
		jsonArray.addAll(lcp);// 鎶奐SONArray娣诲姞List,List<User>杞琂SONArray,鍚屾椂涔熷彲浠ユ槸Map锛孲et绛夐泦鍚�
		String e = jsonArray.toJSONString();
		
		return e;
	}
	//跳转新增培训计划
	@RequestMapping(value="/GetAdd.html")
	public String GetAdd() {
		return "cultvate/cv_add";
	}
	//添加培训计划（未做非空验证，培训讲师需要判断字符串长度）
	@RequestMapping(value="/PlandSave.html")
	public String ps(Cv_Plane cv) {
		int result = cs.AddPland(cv);
		System.out.println("result:"+result);
		return "cultvate/cv_px";
	}
	//跳转培训计划明细
	@RequestMapping(value="cvDetail.html/{id}")
	public String cv_PlandMx(@PathVariable int id,HttpServletRequest request) {
		
		Cv_Plane cp = cs.selectDetailById(id);//根据ID获取培训计划详细信息
		request.setAttribute("cps", cp);
		return "cultvate/cv_mx";
	}
	//跳转培训计划修改业务
	@RequestMapping(value="cvChange.html/{id}")
	public String cv_ChangeById(@PathVariable int id,HttpServletRequest request) {
		Cv_Plane cpchange = cs.selectDetailById(id);//根据ID获取培训计划详细信息
		
		request.setAttribute("cps", cpchange);
		return "cultvate/cv_change";
	}
	//修改后点击保存按钮后业务
	@RequestMapping(value="/SaveNews.html")
	public String cv_SaveChangeNews(Cv_Plane cp) {
		int result = cs.changeCvById(cp);//保存修改后的数据
		return "cultvate/cv_px";
	}
	//更具ID查询培训计划名称
	@RequestMapping(value="SelectPlanName.json",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String cv_SelectNameById(Cv_Plane cv) {
		System.out.println(cv.getId());
		List<Cv_Plane> cvp = cs.selectPlaneNameById(cv.getId());
		for (Cv_Plane c : cvp) {
			System.out.println("名字:"+c.getTrainName());
		}
		JSONArray jsonArray = new JSONArray();// 鍒涘缓JSONArray
		jsonArray.addAll(cvp);// 鎶奐SONArray娣诲姞List,List<User>杞琂SONArray,鍚屾椂涔熷彲浠ユ槸Map锛孲et绛夐泦鍚�
		String a = jsonArray.toJSONString();
		System.out.println(a);
		return a;
	}
	//删除培训计划业务
	@RequestMapping(value="DeletePlan.json",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String cv_DeletePlan(Cv_Plane cv) {
		int result = cs.deletePlandById(cv.getId());
		Map<String, Object> map = new HashMap<String, Object>();
		if(result > 0) {
			map.put("result", "true");
		}else {
			map.put("result", "flase");
		}
		System.out.println("删除值:"+result);
		return JSONArray.toJSONString(map);
		
	}
	//根据ID修改培训计划的状态（申请审核业务）
	@RequestMapping(value="cv_ChangeStats.json",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String changeStarts_Cv(Cv_Plane cp) {
		int result = cs.verification(cp);
		Map<String, Object> map = new HashMap<String,Object>();
		if(result>0) {
			map.put("result", "true");
		}else {
			map.put("result", "false");
		}
		return JSONArray.toJSONString(map);
	}
	//跳转培训反馈页面
	@RequestMapping(value="cvFk.html/{id}")
	public String cv_PlandFk(@PathVariable int id,HttpServletRequest request) {
		Cv_Plane cp = cs.selectDetailById(id);//根据ID获取培训计划详细信息
		List<Cv_Feedback> cf = cs.feedback();
		request.setAttribute("cpf", cf);//培训反馈下拉框
		request.setAttribute("cps", cp);
		return "cultvate/cv_fk";
	}
	//保存培训反馈页面
	@RequestMapping(value="CvSaveFk.json",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String cv_plandsavefk(Cv_Plane cv) {
		Cv_Plane cvs = new Cv_Plane(cv.getId(), cv.getFeedback(), cv.getSummary(), cv.getResult());
		int result = cs.SaveFk(cvs);
		Map<String,Object> map = new HashMap<String,Object>();
		if(result>0) {
			map.put("result", "true");
		}else {
			map.put("result", "flase");
		}
		return JSONArray.toJSONString(map);
		
	}
	//复核跳转页面
	@RequestMapping(value="recheck.html/{id}")
	public String reCheck(@PathVariable int id,HttpServletRequest request){
		Cv_Plane cp = cs.selectDetailById(id);//根据ID获取培训计划详细信息
		request.setAttribute("cps", cp);
		return "cultvate/cv_check";
	}
	//点击审核通过跳转的Controller///////////////////////////////////////////////////
	@RequestMapping(value="/ChangeStarts.json",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String changeStarts(Cv_Plane cp) {
		int result = cs.changeAuditStarts(cp);
		System.out.println("更新返回的值为:"+result);
		System.out.println("进入Controller");
		Map<String, Object> map = new HashMap<String,Object>();
		if(result>0) {
			map.put("result", "true");
		}else {
			map.put("result", "false");
		}
		return JSONArray.toJSONString(map);
	}
	//点击驳回业务cv_Reject.json
	@RequestMapping(value="cv_Reject.json",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String reject_Cv(Cv_Plane cp) {
		int result = cs.cv_Reject(cp);
		Map<String, Object> map = new HashMap<String,Object>();
		if(result>0) {
			map.put("result", "true");
		}else {
			map.put("result", "false");
		}
		return JSONArray.toJSONString(map);
	}
}
